package com.controller;

import com.dao.UserDAO;
import com.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class ResetController {
    private UserDAO userDAO;

    public ResetController(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @GetMapping("/reset")
    public String resetPage() {
        return "reset";
    }

    @PostMapping("/reset")
    public String reset(@RequestParam String account,
                         @RequestParam String newPwd,
                         @RequestParam String surePwd,
                         Model model) {
        // 检查手机号是否已存在
        if (userDAO.findStudentByAccount(account) == null) {
            model.addAttribute("error", "该手机号未注册");
            return "reset";
        } else if (newPwd.equals(surePwd)) {
            // 修改学生对象并保存到数据库
            User updataUser = userDAO.findStudentByAccount(account);
            updataUser.setUserPwd(newPwd);
            userDAO.update(updataUser);
            model.addAttribute("success", "修改成功，请登录");
            return "redirect:/login"; // 假设有一个登录页面
        } else{
            model.addAttribute("error", "两次密码不一致");
            return "reset";
        }
    }
}
